﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace xAsync
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, RoutedEventArgs e)
        {
            //var r1 = await Get1();
            var r2 = await Get2();
        }

        public async Task<JObject> Get1()
        {
            // await client.GetStringAsync 在等待 client.GetStringAsync 返回结果
            // jsonTask.Result 需要立即拿到结果
            // 因此导致死锁
            var r = GetJsonAsync(new Uri("http://127.0.0.1:20015/api/rtsp/getPullUrl?pushMachineId=25655f2315c49b18&pullMachineId=np"));
            return r.Result;
        }

        public async Task<string> Get2()
        {
            var r = await GetJsonAsync(new Uri("http://127.0.0.1:20015/api/rtsp/getPullUrl?pushMachineId=25655f2315c49b18&pullMachineId=np"));
            return r["data"].ToString();
        }

        private async Task<JObject> GetJsonAsync(Uri uri)
        {
            using (var client = new HttpClient())
            {
                var jsonString = await client.GetStringAsync(uri);
                return JObject.Parse(jsonString);
            }
        }
    }
}
